<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 商品分类模型
 *
 * @author www.dingwei.cn
 */

class ConsumerCategoryModel extends MY_Model{
	
	public $tbProductCategory= 'product_category';
	public $tbProductCustomCategory= 'product_custom_category';

	/**
	 * 商家类型分类
	 * @var array
	 * @author zyl
	 */
	public $type = array(
			1 => '商城',
	);
	
	/**
	 * 获取分类信息
	 * @param integer $id	id
	 * @return array|array
	 * @author zyl
	 */
	public function getInfo($id){
		if (! $id){
			return array();
		}
		$this->db->select('id,type,parent_id,cat_name,pic,status,sort')
            ->where('id',$id);
		$tA = $this->getOne($this->tbProductCategory);
		
		return $tA;
	}
	
	
	/**
	 * 获取分类信息
	 * @param integer $id	id
	 * @return array|array
	 * @author zyl
	 */
	public function getInfoByIds($ids){
		if (! $ids){
			return array();
		}
		$this->db->select('id,cat_name')
				 ->where_in('id',$ids);
		$tA = $this->getAll($this->tbProductCategory);
		
		$data = array();
		foreach ($tA as $k=>$v){
			$data[$v['id']] = $v;
		}
		return $data;
	}
	
	/**
	 * 新增 | 修改
	 * @param array $data	数据
	 * @param number $id	id
	 * @return boolean|boolean|number
	 * @author zyl
	 */
	public function save($data ,$id = 0){
		if ($id){
			$this->db->where('id',$id);
			$query = $this->update($this->tbProductCategory, $data);
			return ($query === false ? false : true);
		}else{
			return $this->insert($this->tbProductCategory, $data);
		}
	}
	
	/**
	 * 分类列表
	 * @author zyl
	 */
	public function getCategoryList($parentId = -1,$source = false, $merchantCategoryId = 0, $remove = false){

		if ($parentId >= 0){
			$this->db->where('parent_id',$parentId);
		}else{
            $this->db->where('parent_id',0);
        }
		
		if ($merchantCategoryId){
			$this->db->where('type', $merchantCategoryId);          //增加店铺类型 khj
		}
		if($remove){
		    $this->db->where_not_in('id', array(1,2,3));
        }
		$this->db
		->select('id,cat_name,pic')
		->where('status',2)
		->order_by('sort asc,id desc');
		
		$tA = $this->getAll($this->tbProductCategory);
		
		if ($source){
			return $tA;
		}else {
			$tData = array();
			foreach ($tA as $k=>$v){
				$tData[] = array(
						'id'=>$v['id'],
						'cat_name'=>$v['cat_name'],
						'pic'=>base_url($v['pic']),
				);
			}
			
			return $tData;
		}
	}
	
	
	/////////////////////////////////////////////////////////////////
	//+-------------------------------------------------------------
	//|	商家自定义分类
	//+-------------------------------------------------------------
	
	/**
	 * 新增 | 修改
	 * @param array $data	数据
	 * @param number $id	id
	 * @return boolean|boolean|number
	 * @author zyl
	 */
	public function saveCustom($data ,$id = 0){
		if ($id){
			$this->db->where('id',$id);
			$query = $this->update($this->tbProductCustomCategory, $data);
			return ($query === false ? false : true);
		}else{
			return $this->insert($this->tbProductCustomCategory, $data);
		}
	}

    /**
     * 批量修改自定义分类
     * 新增 | 修改
     * @param array $data	数据
     * @author khj
     */
    public function saveCustomBatch($data , $key = 'id'){
        if( ! $data) return false;

        return $this->updateBatch($this->tbProductCustomCategory, $data, $key);
    }


	/**
	 * 获取分类信息
	 * @param integer $id	id
	 * @param integer $merchantId  商家id
	 * @return array|array
	 * @author zyl
	 */
	public function getCustomInfo($id, $merchantId = 0){
		if (! $id){
			return array();
		}

		if($merchantId){
		    $this->db->where('merchant_id', $merchantId);
        }

		$this->db->select('id,parent_id,category_id,merchant_id,cat_name,status')
            ->where('id',$id);
		$tA = $this->getOne($this->tbProductCustomCategory);
		
		return $tA;
	}

    /**
     * 获取商家自定义分类
     * @param $merchantId   商家id
     * @return array
     */
    public function appGetCustomCategoryList($merchantId, $page){
        if(!$merchantId)return array();

        $this->db->select('id,cat_name')
                 ->where('merchant_id', $merchantId)
                 ->order_by('addtime desc');

        return $this->appPage($this->tbProductCustomCategory, $page);
    }

    /**
     * 获取二级分类下所有商家
     * @param $customId
     * @return array
     */
    public function getAllMerchantByCateId($catId){
        if(!$catId){
            return array();
        }
        get_instance()->loadModel('MerchantUserModel', 'mMerchant','merchant');

        $this->db->select('c.merchant_id,u.nickname')
                ->join($this->mMerchant->tbUser.' u', 'u.id = c.merchant_id')
                ->where('c.category_id', $catId);
        $tA = $this->getAll($this->tbProductCustomCategory.' c');

        return $tA;
    }
	
	/**
	 * 获取商家自定义分类	<通过三种id来获取>
	 * @param number $merchantId	商家id
	 * @param number $categoryId	平台二级分类id
	 * @param number $parentId		一级分类id
	 * @param string $keyword		分类名称关键字
     * @return  array
	 * @author zyl
	 */
	public function getCustomList($merchantId = 0, $categoryId = 0, $parentId = 0 , $keyword = ''){
		if (! $merchantId && ! $categoryId && ! $parentId){
			return array();
		}
		
		if ($merchantId){
			$this->db->where('c.merchant_id',$merchantId);
		}
		
		if ($categoryId){
			$this->db->where('c.category_id',$categoryId);
		}
		
		if ($parentId){
			$this->db->where('c.parent_id',$parentId);
		}
		
		if ($keyword){
			$this->db->like('c.cat_name',$keyword);
		}
        get_instance()->loadModel('MerchantUserModel', 'mMerchant','merchant');
		$this->db->select('c.id,c.cat_name,c.sort')
            ->join($this->mMerchant->tbUser.' u', 'u.id=c.merchant_id')
            ->where('u.status > -1')
                 ->order_by('c.sort asc,c.id desc');
		return $this->getAll($this->tbProductCustomCategory.' c');
	}

    /**
     * 验证商家自定义分类名称是否重复
     * @param $catName   分类名称
     * @param int $merchantId   商家id
     * @param int $id    分类id
     * @return array
     */
    public function getCategoryInfoByName($catName, $merchantId = 0, $id = 0){
        if(!$catName)return array();

        if($id){
            $this->db->where(array('id <>' => $id));
        }


        $this->db->select('id,cat_name')
            ->where('merchant_id', $merchantId)
            ->where('cat_name', $catName);

        return $this->getOne($this->tbProductCustomCategory);
    }
	
    /////////////////////////////////////////////////////////////////
    //+-------------------------------------------------------------
    //|	后台方法
    //+-------------------------------------------------------------
    
	/**
	 * 获取列表
	 * @return array
	 * @author zyl
	 */
    public function adminGetList(){
    	$pid = get('pid');
    	$catName = trim(get('cat_name'));
    	$status = (int)get('status');
        $sendDateFrom = strtotime(get('send_date_from'));
        $sendDateTo = strtotime(get('send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

    	if ($pid){
    		$this->db->where('parent_id',$pid);
    	}else{
    		$this->db->where('parent_id',0);
    	}
    	if ($catName){
    	    $this->db->like(array('cat_name' => $catName));
        }
        if ($status){
    	    $this->db->where(array('status' => $status));
        }
        if($sendDateFrom){
            $this->db->where('addtime >=', $sendDateFrom);
        }
        if($sendDateTo){
            $this->db->where('addtime <=  ', strtotime('+1day', $sendDateTo));
        }
        if ($iSortCol_0 == 3){
            $this->db->order_by('addtime', $sSortDir_0);
        }

    	$this->db->select('id,parent_id,type,cat_name,pic,status,sort,addtime');
    	return $this->pagination($this->tbProductCategory);
    }
    
	/**
	 * 获取列表
     * @param   array       $merchantIds
	 * @return array
	 * @author zyl
	 */
    public function adminGetCustomCategoryList($merchantIds = array()){
    	$pid = get('pid');
    	if (! $pid){
    		return array();
    	}
    	$catName = get('cat_name');
    	$merchantId = get('merchant');
    	$status = get('status');
        $sendDateFrom = strtotime(get('send_date_from'));
        $sendDateTo = strtotime(get('send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if (!empty($merchantIds)){
            $this->db->where_in('merchant_id',$merchantIds);
        }

    	if ($catName){
    		$this->db->like('cat_name',$catName);
    	}
    	if ($status){
    		$this->db->where('status',$status);
    	}
    	if ($merchantId){
    		$this->db->where('merchant_id',$merchantId);
    	}
        if($sendDateFrom){
            $this->db->where('addtime >=', $sendDateFrom);
        }
        if($sendDateTo){
            $this->db->where('addtime <= ', strtotime('+1day', $sendDateTo));
        }
        if ($iSortCol_0 == 3){
            $this->db->order_by('addtime', $sSortDir_0);
        }

    	$this->db->where('category_id',$pid);
    	$this->db->select('id,category_id,cat_name,merchant_id,county_id,status,addtime');
    	return $this->pagination($this->tbProductCustomCategory);
    }

    /**
     * 通过商户获取列表
     * @return array
     * @author zyl
     */
    public function adminGetCustomCategoryListByMerchantId(){
        $id = (int)get('id');
        if (!$id){
            return array();
        }
        $catName = get('cat_name');
        $parentName = get('parent_name');
        $categoryName = get('category_name');
        $catStatus = get('cat_status');
        $sendDateFrom = strtotime(get('cat_send_date_from'));
        $sendDateTo = strtotime(get('cat_send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if ($categoryName){
            $this->db->like('cat.cat_name',$categoryName);
        }
        if ($parentName){
            $this->db->like('c.cat_name',$parentName);
        }
        if ($catName){
            $this->db->like('cc.cat_name',$catName);
        }
        if ($catStatus){
            $this->db->where('cc.status',$catStatus);
        }
        if($sendDateFrom){
            $this->db->where('cc.addtime >=', $sendDateFrom);
        }
        if($sendDateTo){
            $this->db->where('cc.addtime <= ', strtotime('+1day', $sendDateTo));
        }
        if ($iSortCol_0 == 5){
            $this->db->order_by('cc.addtime', $sSortDir_0);
        }

        $this->db->where(array('merchant_id' => $id));
        $this->db->select('cc.id,cc.merchant_id,cc.cat_name,cc.status,cc.addtime,c.cat_name as parent_name,
            cat.cat_name as category_name')
            ->join($this->tbProductCategory.' c', 'cc.parent_id=c.id')
            ->join($this->tbProductCategory.' cat', 'cc.category_id=cat.id');
        return $this->pagination($this->tbProductCustomCategory.' cc');
    }

    /**
     * 删除自定义分类
     * @param $id
     * @param $merchantId  商家id  @author khj
     * @return bool
     * @author heyu
     */
    public function deleteCustomCategory($id, $merchantId = 0){
        if (!$id){
            return false;
        }
        $this->db->where('merchant_id', $merchantId);
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbProductCustomCategory);
    }

    /**
     * 删除商品分类
     * @param $id
     * @return bool
     * @author heyu
     */
    public function deleteCategory($id){
        if (!$id){
            return false;
        }
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbProductCategory);
    }
}